package com.str.stack;
public class LinkedStack implements Stack {

// Inner class
  private static class SLLNode {
    Object data;
    SLLNode next;

    SLLNode() {}

    SLLNode(Object obj)
    { data = obj; }

    SLLNode(Object obj, SLLNode link) {
      data = obj;
      next = link;
    }

    public String toString()
    { return data.toString(); }
  }

  private SLLNode top; // reference to top Node
  private int count; // number of items on Stack

// Constructor
  public LinkedStack()
  {	clear(); }

// Modifiers
  public final void clear() {
    top = null;
    count = 0;
  }

  public void push(Object val) {
    top = new SLLNode(val, top);
    count++;
  }

  public Object pop() {
    if (top == null)
      throw new IllegalStateException();
    Object val = top.data;
    top = top.next;
    count--;
    return val;
  }

// Accessors
  public boolean isEmpty() 
  { return top == null; }

  public int size() 
  {	return count; }

  public Object peek() {
    if (top == null)
      throw new IllegalStateException();
    return top.data;
  }

  public String toString() {
    String buf = "[ ";
    for (SLLNode curr = top; curr != null; 
        curr = curr.next) {
      if (curr != top) buf += ", ";
      buf += curr.data;
    }
    buf += " ]";
    return buf;
  }
}
